#ifndef __ATA_H__
#define __ATA_H__
#include <cnix/dma.h>
#include <cnix/scsi.h>

/*
 *FIXME!
 *These constants are rather ad-hoc.
 *We need a much more generic approach here.
 */
#define ATA_BUS_DOMAIN	0x0
#define ATA_BUS_NR			0x0
#define ATA_BUS_DEVFN		((0x7 << 3)|0x1)

/* defines only for the constants which don't work well as enums */
#define ATA_DMA_BOUNDARY	0xffffUL
#define ATA_DMA_MASK		0xffffffffULL

/* libata-core.c */
enum {  
  /* flags for ata_dev_read_id() */
  ATA_READID_POSTRESET  = (1 << 0), /* reading ID after reset */

  /* selector for ata_down_xfermask_limit() */
  ATA_DNXFER_PIO    = 0,  /* speed down PIO */
  ATA_DNXFER_DMA    = 1,  /* speed down DMA */
  ATA_DNXFER_40C    = 2,  /* apply 40c cable limit */
  ATA_DNXFER_FORCE_PIO  = 3,  /* force PIO */
  ATA_DNXFER_FORCE_PIO0 = 4,  /* force PIO0 */

  ATA_DNXFER_QUIET  = (1 << 31),
};

enum {
	/* various global constants */
	ATA_MAX_DEVICES		= 2,	/* per bus/port */
	ATA_MAX_PRD		= 256,	/* we could make these 256/256 */
	ATA_SECT_SIZE		= 512,
	ATA_MAX_SECTORS_128	= 128,
	ATA_MAX_SECTORS		= 256,
	ATA_MAX_SECTORS_LBA48	= 65535,/* TODO: 65536? */
	ATA_MAX_SECTORS_TAPE	= 65535,

	ATA_ID_WORDS		= 256,
	ATA_ID_CONFIG		= 0,
	ATA_ID_CYLS		= 1,
	ATA_ID_HEADS		= 3,
	ATA_ID_SECTORS		= 6,
	ATA_ID_SERNO		= 10,
	ATA_ID_BUF_SIZE		= 21,
	ATA_ID_FW_REV		= 23,
	ATA_ID_PROD		= 27,
	ATA_ID_MAX_MULTSECT	= 47,
	ATA_ID_DWORD_IO		= 48,
	ATA_ID_CAPABILITY	= 49,
	ATA_ID_OLD_PIO_MODES	= 51,
	ATA_ID_OLD_DMA_MODES	= 52,
	ATA_ID_FIELD_VALID	= 53,
	ATA_ID_CUR_CYLS		= 54,
	ATA_ID_CUR_HEADS	= 55,
	ATA_ID_CUR_SECTORS	= 56,
	ATA_ID_MULTSECT		= 59,
	ATA_ID_LBA_CAPACITY	= 60,
	ATA_ID_SWDMA_MODES	= 62,
	ATA_ID_MWDMA_MODES	= 63,
	ATA_ID_PIO_MODES	= 64,
	ATA_ID_EIDE_DMA_MIN	= 65,
	ATA_ID_EIDE_DMA_TIME	= 66,
	ATA_ID_EIDE_PIO		= 67,
	ATA_ID_EIDE_PIO_IORDY	= 68,
	ATA_ID_ADDITIONAL_SUPP	= 69,
	ATA_ID_QUEUE_DEPTH	= 75,
	ATA_ID_MAJOR_VER	= 80,
	ATA_ID_COMMAND_SET_1	= 82,
	ATA_ID_COMMAND_SET_2	= 83,
	ATA_ID_CFSSE		= 84,
	ATA_ID_CFS_ENABLE_1	= 85,
	ATA_ID_CFS_ENABLE_2	= 86,
	ATA_ID_CSF_DEFAULT	= 87,
	ATA_ID_UDMA_MODES	= 88,
	ATA_ID_HW_CONFIG	= 93,
	ATA_ID_SPG		= 98,
	ATA_ID_LBA_CAPACITY_2	= 100,
	ATA_ID_SECTOR_SIZE	= 106,
	ATA_ID_LAST_LUN		= 126,
	ATA_ID_DLF		= 128,
	ATA_ID_CSFO		= 129,
	ATA_ID_CFA_POWER	= 160,
	ATA_ID_CFA_KEY_MGMT	= 162,
	ATA_ID_CFA_MODES	= 163,
	ATA_ID_DATA_SET_MGMT	= 169,
	ATA_ID_ROT_SPEED	= 217,
	ATA_ID_PIO4		= (1 << 1),

	ATA_ID_SERNO_LEN	= 20,
	ATA_ID_FW_REV_LEN	= 8,
	ATA_ID_PROD_LEN		= 40,

	ATA_PCI_CTL_OFS		= 2,

	ATA_PIO0		= (1 << 0),
	ATA_PIO1		= ATA_PIO0 | (1 << 1),
	ATA_PIO2		= ATA_PIO1 | (1 << 2),
	ATA_PIO3		= ATA_PIO2 | (1 << 3),
	ATA_PIO4		= ATA_PIO3 | (1 << 4),
	ATA_PIO5		= ATA_PIO4 | (1 << 5),
	ATA_PIO6		= ATA_PIO5 | (1 << 6),

	ATA_PIO4_ONLY		= (1 << 4),

	ATA_SWDMA0		= (1 << 0),
	ATA_SWDMA1		= ATA_SWDMA0 | (1 << 1),
	ATA_SWDMA2		= ATA_SWDMA1 | (1 << 2),

	ATA_SWDMA2_ONLY		= (1 << 2),

	ATA_MWDMA0		= (1 << 0),
	ATA_MWDMA1		= ATA_MWDMA0 | (1 << 1),
	ATA_MWDMA2		= ATA_MWDMA1 | (1 << 2),
	ATA_MWDMA3		= ATA_MWDMA2 | (1 << 3),
	ATA_MWDMA4		= ATA_MWDMA3 | (1 << 4),

	ATA_MWDMA12_ONLY	= (1 << 1) | (1 << 2),
	ATA_MWDMA2_ONLY		= (1 << 2),

	ATA_UDMA0		= (1 << 0),
	ATA_UDMA1		= ATA_UDMA0 | (1 << 1),
	ATA_UDMA2		= ATA_UDMA1 | (1 << 2),
	ATA_UDMA3		= ATA_UDMA2 | (1 << 3),
	ATA_UDMA4		= ATA_UDMA3 | (1 << 4),
	ATA_UDMA5		= ATA_UDMA4 | (1 << 5),
	ATA_UDMA6		= ATA_UDMA5 | (1 << 6),
	ATA_UDMA7		= ATA_UDMA6 | (1 << 7),
	/* ATA_UDMA7 is just for completeness... doesn't exist (yet?).  */

	ATA_UDMA24_ONLY		= (1 << 2) | (1 << 4),

	ATA_UDMA_MASK_40C	= ATA_UDMA2,	/* udma0-2 */

	/* DMA-related */
	ATA_PRD_SZ		= 8,
	ATA_PRD_TBL_SZ		= (ATA_MAX_PRD * ATA_PRD_SZ),
	ATA_PRD_EOT		= (1 << 31),	/* end-of-table flag */

	ATA_DMA_TABLE_OFS	= 4,
	ATA_DMA_STATUS		= 2,
	ATA_DMA_CMD		= 0,
	ATA_DMA_WR		= (1 << 3),
	ATA_DMA_START		= (1 << 0),
	ATA_DMA_INTR		= (1 << 2),
	ATA_DMA_ERR		= (1 << 1),
	ATA_DMA_ACTIVE		= (1 << 0),

	/* bits in ATA command block registers */
	ATA_HOB			= (1 << 7),	/* LBA48 selector */
	ATA_NIEN		= (1 << 1),	/* disable-irq flag */
	ATA_LBA			= (1 << 6),	/* LBA28 selector */
	ATA_DEV1		= (1 << 4),	/* Select Device 1 (slave) */
	ATA_DEVICE_OBS		= (1 << 7) | (1 << 5), /* obs bits in dev reg */
	ATA_DEVCTL_OBS		= (1 << 3),	/* obsolete bit in devctl reg */
	ATA_BUSY		= (1 << 7),	/* BSY status bit */
	ATA_DRDY		= (1 << 6),	/* device ready */
	ATA_DF			= (1 << 5),	/* device fault */
	ATA_DSC			= (1 << 4),	/* drive seek complete */
	ATA_DRQ			= (1 << 3),	/* data request i/o */
	ATA_CORR		= (1 << 2),	/* corrected data error */
	ATA_IDX			= (1 << 1),	/* index */
	ATA_ERR			= (1 << 0),	/* have an error */
	ATA_SRST		= (1 << 2),	/* software reset */
	ATA_ICRC		= (1 << 7),	/* interface CRC error */
	ATA_BBK			= ATA_ICRC,	/* pre-EIDE: block marked bad */
	ATA_UNC			= (1 << 6),	/* uncorrectable media error */
	ATA_MC			= (1 << 5),	/* media changed */
	ATA_IDNF		= (1 << 4),	/* ID not found */
	ATA_MCR			= (1 << 3),	/* media change requested */
	ATA_ABORTED		= (1 << 2),	/* command aborted */
	ATA_TRK0NF		= (1 << 1),	/* track 0 not found */
	ATA_AMNF		= (1 << 0),	/* address mark not found */
	ATAPI_LFS		= 0xF0,		/* last failed sense */
	ATAPI_EOM		= ATA_TRK0NF,	/* end of media */
	ATAPI_ILI		= ATA_AMNF,	/* illegal length indication */
	ATAPI_IO		= (1 << 1),
	ATAPI_COD		= (1 << 0),

	/* ATA command block registers */
	ATA_REG_DATA		= 0x00,
	ATA_REG_ERR		= 0x01,
	ATA_REG_NSECT		= 0x02,
	ATA_REG_LBAL		= 0x03,
	ATA_REG_LBAM		= 0x04,
	ATA_REG_LBAH		= 0x05,
	ATA_REG_DEVICE		= 0x06,
	ATA_REG_STATUS		= 0x07,

	ATA_REG_FEATURE		= ATA_REG_ERR, /* and their aliases */
	ATA_REG_CMD		= ATA_REG_STATUS,
	ATA_REG_BYTEL		= ATA_REG_LBAM,
	ATA_REG_BYTEH		= ATA_REG_LBAH,
	ATA_REG_DEVSEL		= ATA_REG_DEVICE,
	ATA_REG_IRQ		= ATA_REG_NSECT,

	/* ATA device commands */
	ATA_CMD_DEV_RESET	= 0x08, /* ATAPI device reset */
	ATA_CMD_CHK_POWER	= 0xE5, /* check power mode */
	ATA_CMD_STANDBY		= 0xE2, /* place in standby power mode */
	ATA_CMD_IDLE		= 0xE3, /* place in idle power mode */
	ATA_CMD_EDD		= 0x90,	/* execute device diagnostic */
	ATA_CMD_DOWNLOAD_MICRO  = 0x92,
	ATA_CMD_NOP		= 0x00,
	ATA_CMD_FLUSH		= 0xE7,
	ATA_CMD_FLUSH_EXT	= 0xEA,
	ATA_CMD_ID_ATA		= 0xEC,
	ATA_CMD_ID_ATAPI	= 0xA1,
	ATA_CMD_SERVICE		= 0xA2,
	ATA_CMD_READ		= 0xC8,
	ATA_CMD_READ_EXT	= 0x25,
	ATA_CMD_READ_QUEUED	= 0x26,
	ATA_CMD_READ_STREAM_EXT	= 0x2B,
	ATA_CMD_READ_STREAM_DMA_EXT = 0x2A,
	ATA_CMD_WRITE		= 0xCA,
	ATA_CMD_WRITE_EXT	= 0x35,
	ATA_CMD_WRITE_QUEUED	= 0x36,
	ATA_CMD_WRITE_STREAM_EXT = 0x3B,
	ATA_CMD_WRITE_STREAM_DMA_EXT = 0x3A,
	ATA_CMD_WRITE_FUA_EXT	= 0x3D,
	ATA_CMD_WRITE_QUEUED_FUA_EXT = 0x3E,
	ATA_CMD_FPDMA_READ	= 0x60,
	ATA_CMD_FPDMA_WRITE	= 0x61,
	ATA_CMD_PIO_READ	= 0x20,
	ATA_CMD_PIO_READ_EXT	= 0x24,
	ATA_CMD_PIO_WRITE	= 0x30,
	ATA_CMD_PIO_WRITE_EXT	= 0x34,
	ATA_CMD_READ_MULTI	= 0xC4,
	ATA_CMD_READ_MULTI_EXT	= 0x29,
	ATA_CMD_WRITE_MULTI	= 0xC5,
	ATA_CMD_WRITE_MULTI_EXT	= 0x39,
	ATA_CMD_WRITE_MULTI_FUA_EXT = 0xCE,
	ATA_CMD_SET_FEATURES	= 0xEF,
	ATA_CMD_SET_MULTI	= 0xC6,
	ATA_CMD_PACKET		= 0xA0,
	ATA_CMD_VERIFY		= 0x40,
	ATA_CMD_VERIFY_EXT	= 0x42,
	ATA_CMD_WRITE_UNCORR_EXT = 0x45,
	ATA_CMD_STANDBYNOW1	= 0xE0,
	ATA_CMD_IDLEIMMEDIATE	= 0xE1,
	ATA_CMD_SLEEP		= 0xE6,
	ATA_CMD_INIT_DEV_PARAMS	= 0x91,
	ATA_CMD_READ_NATIVE_MAX	= 0xF8,
	ATA_CMD_READ_NATIVE_MAX_EXT = 0x27,
	ATA_CMD_SET_MAX		= 0xF9,
	ATA_CMD_SET_MAX_EXT	= 0x37,
	ATA_CMD_READ_LOG_EXT	= 0x2F,
	ATA_CMD_WRITE_LOG_EXT	= 0x3F,
	ATA_CMD_READ_LOG_DMA_EXT = 0x47,
	ATA_CMD_WRITE_LOG_DMA_EXT = 0x57,
	ATA_CMD_TRUSTED_RCV	= 0x5C,
	ATA_CMD_TRUSTED_RCV_DMA = 0x5D,
	ATA_CMD_TRUSTED_SND	= 0x5E,
	ATA_CMD_TRUSTED_SND_DMA = 0x5F,
	ATA_CMD_PMP_READ	= 0xE4,
	ATA_CMD_PMP_WRITE	= 0xE8,
	ATA_CMD_CONF_OVERLAY	= 0xB1,
	ATA_CMD_SEC_SET_PASS	= 0xF1,
	ATA_CMD_SEC_UNLOCK	= 0xF2,
	ATA_CMD_SEC_ERASE_PREP	= 0xF3,
	ATA_CMD_SEC_ERASE_UNIT	= 0xF4,
	ATA_CMD_SEC_FREEZE_LOCK	= 0xF5,
	ATA_CMD_SEC_DISABLE_PASS = 0xF6,
	ATA_CMD_CONFIG_STREAM	= 0x51,
	ATA_CMD_SMART		= 0xB0,
	ATA_CMD_MEDIA_LOCK	= 0xDE,
	ATA_CMD_MEDIA_UNLOCK	= 0xDF,
	ATA_CMD_DSM		= 0x06,
	ATA_CMD_CHK_MED_CRD_TYP = 0xD1,
	ATA_CMD_CFA_REQ_EXT_ERR = 0x03,
	ATA_CMD_CFA_WRITE_NE	= 0x38,
	ATA_CMD_CFA_TRANS_SECT	= 0x87,
	ATA_CMD_CFA_ERASE	= 0xC0,
	ATA_CMD_CFA_WRITE_MULT_NE = 0xCD,
	/* marked obsolete in the ATA/ATAPI-7 spec */
	ATA_CMD_RESTORE		= 0x10,

	/* READ_LOG_EXT pages */
	ATA_LOG_SATA_NCQ	= 0x10,

	/* READ/WRITE LONG (obsolete) */
	ATA_CMD_READ_LONG	= 0x22,
	ATA_CMD_READ_LONG_ONCE	= 0x23,
	ATA_CMD_WRITE_LONG	= 0x32,
	ATA_CMD_WRITE_LONG_ONCE	= 0x33,

	/* SETFEATURES stuff */
	SETFEATURES_XFER	= 0x03,
	XFER_UDMA_7		= 0x47,
	XFER_UDMA_6		= 0x46,
	XFER_UDMA_5		= 0x45,
	XFER_UDMA_4		= 0x44,
	XFER_UDMA_3		= 0x43,
	XFER_UDMA_2		= 0x42,
	XFER_UDMA_1		= 0x41,
	XFER_UDMA_0		= 0x40,
	XFER_MW_DMA_4		= 0x24,	/* CFA only */
	XFER_MW_DMA_3		= 0x23,	/* CFA only */
	XFER_MW_DMA_2		= 0x22,
	XFER_MW_DMA_1		= 0x21,
	XFER_MW_DMA_0		= 0x20,
	XFER_SW_DMA_2		= 0x12,
	XFER_SW_DMA_1		= 0x11,
	XFER_SW_DMA_0		= 0x10,
	XFER_PIO_6		= 0x0E,	/* CFA only */
	XFER_PIO_5		= 0x0D,	/* CFA only */
	XFER_PIO_4		= 0x0C,
	XFER_PIO_3		= 0x0B,
	XFER_PIO_2		= 0x0A,
	XFER_PIO_1		= 0x09,
	XFER_PIO_0		= 0x08,
	XFER_PIO_SLOW		= 0x00,

	SETFEATURES_WC_ON	= 0x02, /* Enable write cache */
	SETFEATURES_WC_OFF	= 0x82, /* Disable write cache */

	/* Enable/Disable Automatic Acoustic Management */
	SETFEATURES_AAM_ON	= 0x42,
	SETFEATURES_AAM_OFF	= 0xC2,

	SETFEATURES_SPINUP	= 0x07, /* Spin-up drive */

	SETFEATURES_SATA_ENABLE = 0x10, /* Enable use of SATA feature */
	SETFEATURES_SATA_DISABLE = 0x90, /* Disable use of SATA feature */

	/* SETFEATURE Sector counts for SATA features */
	SATA_FPDMA_OFFSET	= 0x01,	/* FPDMA non-zero buffer offsets */
	SATA_FPDMA_AA		= 0x02, /* FPDMA Setup FIS Auto-Activate */
	SATA_DIPM		= 0x03,	/* Device Initiated Power Management */
	SATA_FPDMA_IN_ORDER	= 0x04,	/* FPDMA in-order data delivery */
	SATA_AN			= 0x05,	/* Asynchronous Notification */
	SATA_SSP		= 0x06,	/* Software Settings Preservation */

	/* feature values for SET_MAX */
	ATA_SET_MAX_ADDR	= 0x00,
	ATA_SET_MAX_PASSWD	= 0x01,
	ATA_SET_MAX_LOCK	= 0x02,
	ATA_SET_MAX_UNLOCK	= 0x03,
	ATA_SET_MAX_FREEZE_LOCK	= 0x04,

	/* feature values for DEVICE CONFIGURATION OVERLAY */
	ATA_DCO_RESTORE		= 0xC0,
	ATA_DCO_FREEZE_LOCK	= 0xC1,
	ATA_DCO_IDENTIFY	= 0xC2,
	ATA_DCO_SET		= 0xC3,

	/* feature values for SMART */
	ATA_SMART_ENABLE	= 0xD8,
	ATA_SMART_READ_VALUES	= 0xD0,
	ATA_SMART_READ_THRESHOLDS = 0xD1,

	/* feature values for Data Set Management */
	ATA_DSM_TRIM		= 0x01,

	/* password used in LBA Mid / LBA High for executing SMART commands */
	ATA_SMART_LBAM_PASS	= 0x4F,
	ATA_SMART_LBAH_PASS	= 0xC2,

	/* ATAPI stuff */
	ATAPI_PKT_DMA		= (1 << 0),
	ATAPI_DMADIR		= (1 << 2),	/* ATAPI data dir:
						   0=to device, 1=to host */
	ATAPI_CDB_LEN		= 16,

	/* PMP stuff */
	SATA_PMP_MAX_PORTS	= 15,
	SATA_PMP_CTRL_PORT	= 15,

	SATA_PMP_GSCR_DWORDS	= 128,
	SATA_PMP_GSCR_PROD_ID	= 0,
	SATA_PMP_GSCR_REV	= 1,
	SATA_PMP_GSCR_PORT_INFO	= 2,
	SATA_PMP_GSCR_ERROR	= 32,
	SATA_PMP_GSCR_ERROR_EN	= 33,
	SATA_PMP_GSCR_FEAT	= 64,
	SATA_PMP_GSCR_FEAT_EN	= 96,

	SATA_PMP_PSCR_STATUS	= 0,
	SATA_PMP_PSCR_ERROR	= 1,
	SATA_PMP_PSCR_CONTROL	= 2,

	SATA_PMP_FEAT_BIST	= (1 << 0),
	SATA_PMP_FEAT_PMREQ	= (1 << 1),
	SATA_PMP_FEAT_DYNSSC	= (1 << 2),
	SATA_PMP_FEAT_NOTIFY	= (1 << 3),

	/* cable types */
	ATA_CBL_NONE		= 0,
	ATA_CBL_PATA40		= 1,
	ATA_CBL_PATA80		= 2,
	ATA_CBL_PATA40_SHORT	= 3,	/* 40 wire cable to high UDMA spec */
	ATA_CBL_PATA_UNK	= 4,	/* don't know, maybe 80c? */
	ATA_CBL_PATA_IGN	= 5,	/* don't know, ignore cable handling */
	ATA_CBL_SATA		= 6,

	/* SATA Status and Control Registers */
	SCR_STATUS		= 0,
	SCR_ERROR		= 1,
	SCR_CONTROL		= 2,
	SCR_ACTIVE		= 3,
	SCR_NOTIFICATION	= 4,

	/* SError bits */
	SERR_DATA_RECOVERED	= (1 << 0), /* recovered data error */
	SERR_COMM_RECOVERED	= (1 << 1), /* recovered comm failure */
	SERR_DATA		= (1 << 8), /* unrecovered data error */
	SERR_PERSISTENT		= (1 << 9), /* persistent data/comm error */
	SERR_PROTOCOL		= (1 << 10), /* protocol violation */
	SERR_INTERNAL		= (1 << 11), /* host internal error */
	SERR_PHYRDY_CHG		= (1 << 16), /* PHY RDY changed */
	SERR_PHY_INT_ERR	= (1 << 17), /* PHY internal error */
	SERR_COMM_WAKE		= (1 << 18), /* Comm wake */
	SERR_10B_8B_ERR		= (1 << 19), /* 10b to 8b decode error */
	SERR_DISPARITY		= (1 << 20), /* Disparity */
	SERR_CRC		= (1 << 21), /* CRC error */
	SERR_HANDSHAKE		= (1 << 22), /* Handshake error */
	SERR_LINK_SEQ_ERR	= (1 << 23), /* Link sequence error */
	SERR_TRANS_ST_ERROR	= (1 << 24), /* Transport state trans. error */
	SERR_UNRECOG_FIS	= (1 << 25), /* Unrecognized FIS */
	SERR_DEV_XCHG		= (1 << 26), /* device exchanged */

	/* struct ata_taskfile flags */
	ATA_TFLAG_LBA48		= (1 << 0), /* enable 48-bit LBA and "HOB" */
	ATA_TFLAG_ISADDR	= (1 << 1), /* enable r/w to nsect/lba regs */
	ATA_TFLAG_DEVICE	= (1 << 2), /* enable r/w to device reg */
	ATA_TFLAG_WRITE		= (1 << 3), /* data dir: host->dev==1 (write) */
	ATA_TFLAG_LBA		= (1 << 4), /* enable LBA */
	ATA_TFLAG_FUA		= (1 << 5), /* enable FUA */
	ATA_TFLAG_POLLING	= (1 << 6), /* set nIEN to 1 and use polling */

	/* protocol flags */
	ATA_PROT_FLAG_PIO	= (1 << 0), /* is PIO */
	ATA_PROT_FLAG_DMA	= (1 << 1), /* is DMA */
	ATA_PROT_FLAG_DATA	= ATA_PROT_FLAG_PIO | ATA_PROT_FLAG_DMA,
	ATA_PROT_FLAG_NCQ	= (1 << 2), /* is NCQ */
	ATA_PROT_FLAG_ATAPI	= (1 << 3), /* is ATAPI */
};

enum {
	/* various global constants */
	LIBATA_MAX_PRD		= ATA_MAX_PRD / 2,
	LIBATA_DUMB_MAX_PRD	= ATA_MAX_PRD / 4,	/* Worst case */
	ATA_DEF_QUEUE		= 1,
	/* tag ATA_MAX_QUEUE - 1 is reserved for internal commands */
	ATA_MAX_QUEUE		= 32,
	ATA_TAG_INTERNAL	= ATA_MAX_QUEUE - 1,
	ATA_SHORT_PAUSE		= 16,

	ATAPI_MAX_DRAIN		= 16 << 10,

	ATA_ALL_DEVICES		= (1 << ATA_MAX_DEVICES) - 1,

	ATA_SHT_EMULATED	= 1,
	ATA_SHT_CMD_PER_LUN	= 1,
	ATA_SHT_THIS_ID		= -1,
	ATA_SHT_USE_CLUSTERING	= 1,

	/* struct ata_device stuff */
	ATA_DFLAG_LBA		= (1 << 0), /* device supports LBA */
	ATA_DFLAG_LBA48		= (1 << 1), /* device supports LBA48 */
	ATA_DFLAG_CDB_INTR	= (1 << 2), /* device asserts INTRQ when ready for CDB */
	ATA_DFLAG_NCQ		= (1 << 3), /* device supports NCQ */
	ATA_DFLAG_FLUSH_EXT	= (1 << 4), /* do FLUSH_EXT instead of FLUSH */
	ATA_DFLAG_ACPI_PENDING	= (1 << 5), /* ACPI resume action pending */
	ATA_DFLAG_ACPI_FAILED	= (1 << 6), /* ACPI on devcfg has failed */
	ATA_DFLAG_AN		= (1 << 7), /* AN configured */
	ATA_DFLAG_HIPM		= (1 << 8), /* device supports HIPM */
	ATA_DFLAG_DIPM		= (1 << 9), /* device supports DIPM */
	ATA_DFLAG_DMADIR	= (1 << 10), /* device requires DMADIR */
	ATA_DFLAG_CFG_MASK	= (1 << 12) - 1,

	ATA_DFLAG_PIO		= (1 << 12), /* device limited to PIO mode */
	ATA_DFLAG_NCQ_OFF	= (1 << 13), /* device limited to non-NCQ mode */
	ATA_DFLAG_SLEEPING	= (1 << 15), /* device is sleeping */
	ATA_DFLAG_DUBIOUS_XFER	= (1 << 16), /* data transfer not verified */
	ATA_DFLAG_NO_UNLOAD	= (1 << 17), /* device doesn't support unload */
	ATA_DFLAG_INIT_MASK	= (1 << 24) - 1,

	ATA_DFLAG_DETACH	= (1 << 24),
	ATA_DFLAG_DETACHED	= (1 << 25),

	ATA_DEV_UNKNOWN		= 0,	/* unknown device */
	ATA_DEV_ATA		= 1,	/* ATA device */
	ATA_DEV_ATA_UNSUP	= 2,	/* ATA device (unsupported) */
	ATA_DEV_ATAPI		= 3,	/* ATAPI device */
	ATA_DEV_ATAPI_UNSUP	= 4,	/* ATAPI device (unsupported) */
	ATA_DEV_PMP		= 5,	/* SATA port multiplier */
	ATA_DEV_PMP_UNSUP	= 6,	/* SATA port multiplier (unsupported) */
	ATA_DEV_SEMB		= 7,	/* SEMB */
	ATA_DEV_SEMB_UNSUP	= 8,	/* SEMB (unsupported) */
	ATA_DEV_NONE		= 9,	/* no device */

	/* struct ata_link flags */
	ATA_LFLAG_NO_HRST	= (1 << 1), /* avoid hardreset */
	ATA_LFLAG_NO_SRST	= (1 << 2), /* avoid softreset */
	ATA_LFLAG_ASSUME_ATA	= (1 << 3), /* assume ATA class */
	ATA_LFLAG_ASSUME_SEMB	= (1 << 4), /* assume SEMB class */
	ATA_LFLAG_ASSUME_CLASS	= ATA_LFLAG_ASSUME_ATA | ATA_LFLAG_ASSUME_SEMB,
	ATA_LFLAG_NO_RETRY	= (1 << 5), /* don't retry this link */
	ATA_LFLAG_DISABLED	= (1 << 6), /* link is disabled */
	ATA_LFLAG_SW_ACTIVITY	= (1 << 7), /* keep activity stats */

	/* struct ata_port flags */
	ATA_FLAG_SLAVE_POSS	= (1 << 0), /* host supports slave dev */
					    /* (doesn't imply presence) */
	ATA_FLAG_SATA		= (1 << 1),
	ATA_FLAG_NO_LEGACY	= (1 << 2), /* no legacy mode check */
	ATA_FLAG_MMIO		= (1 << 3), /* use MMIO, not PIO */
	ATA_FLAG_SRST		= (1 << 4), /* (obsolete) use ATA SRST, not E.D.D. */
	ATA_FLAG_SATA_RESET	= (1 << 5), /* (obsolete) use COMRESET */
	ATA_FLAG_NO_ATAPI	= (1 << 6), /* No ATAPI support */
	ATA_FLAG_PIO_DMA	= (1 << 7), /* PIO cmds via DMA */
	ATA_FLAG_PIO_LBA48	= (1 << 8), /* Host DMA engine is LBA28 only */
	ATA_FLAG_PIO_POLLING	= (1 << 9), /* use polling PIO if LLD
					     * doesn't handle PIO interrupts */
	ATA_FLAG_NCQ		= (1 << 10), /* host supports NCQ */
	ATA_FLAG_NO_POWEROFF_SPINDOWN = (1 << 11), /* don't spindown before poweroff */
	ATA_FLAG_NO_HIBERNATE_SPINDOWN = (1 << 12), /* don't spindown before hibernation */
	ATA_FLAG_DEBUGMSG	= (1 << 13),
	ATA_FLAG_FPDMA_AA		= (1 << 14), /* driver supports Auto-Activate */
	ATA_FLAG_IGN_SIMPLEX	= (1 << 15), /* ignore SIMPLEX */
	ATA_FLAG_NO_IORDY	= (1 << 16), /* controller lacks iordy */
	ATA_FLAG_ACPI_SATA	= (1 << 17), /* need native SATA ACPI layout */
	ATA_FLAG_AN		= (1 << 18), /* controller supports AN */
	ATA_FLAG_PMP		= (1 << 19), /* controller supports PMP */
	ATA_FLAG_IPM		= (1 << 20), /* driver can handle IPM */
	ATA_FLAG_EM		= (1 << 21), /* driver supports enclosure
					      * management */
	ATA_FLAG_SW_ACTIVITY	= (1 << 22), /* driver supports sw activity
					      * led */

	/* The following flag belongs to ap->pflags but is kept in
	 * ap->flags because it's referenced in many LLDs and will be
	 * removed in not-too-distant future.
	 */
	ATA_FLAG_DISABLED	= (1 << 23), /* port is disabled, ignore it */

	/* bits 24:31 of ap->flags are reserved for LLD specific flags */


	/* struct ata_port pflags */
	ATA_PFLAG_EH_PENDING	= (1 << 0), /* EH pending */
	ATA_PFLAG_EH_IN_PROGRESS = (1 << 1), /* EH in progress */
	ATA_PFLAG_FROZEN	= (1 << 2), /* port is frozen */
	ATA_PFLAG_RECOVERED	= (1 << 3), /* recovery action performed */
	ATA_PFLAG_LOADING	= (1 << 4), /* boot/loading probe */
	ATA_PFLAG_SCSI_HOTPLUG	= (1 << 6), /* SCSI hotplug scheduled */
	ATA_PFLAG_INITIALIZING	= (1 << 7), /* being initialized, don't touch */
	ATA_PFLAG_RESETTING	= (1 << 8), /* reset in progress */
	ATA_PFLAG_UNLOADING	= (1 << 9), /* driver is being unloaded */
	ATA_PFLAG_UNLOADED	= (1 << 10), /* driver is unloaded */

	ATA_PFLAG_SUSPENDED	= (1 << 17), /* port is suspended (power) */
	ATA_PFLAG_PM_PENDING	= (1 << 18), /* PM operation pending */
	ATA_PFLAG_INIT_GTM_VALID = (1 << 19), /* initial gtm data valid */

	ATA_PFLAG_PIO32		= (1 << 20),  /* 32bit PIO */
	ATA_PFLAG_PIO32CHANGE	= (1 << 21),  /* 32bit PIO can be turned on/off */

	/* struct ata_queued_cmd flags */
	ATA_QCFLAG_ACTIVE	= (1 << 0), /* cmd not yet ack'd to scsi lyer */
	ATA_QCFLAG_DMAMAP	= (1 << 1), /* SG table is DMA mapped */
	ATA_QCFLAG_IO		= (1 << 3), /* standard IO command */
	ATA_QCFLAG_RESULT_TF	= (1 << 4), /* result TF requested */
	ATA_QCFLAG_CLEAR_EXCL	= (1 << 5), /* clear excl_link on completion */
	ATA_QCFLAG_QUIET	= (1 << 6), /* don't report device error */
	ATA_QCFLAG_RETRY	= (1 << 7), /* retry after failure */

	ATA_QCFLAG_FAILED	= (1 << 16), /* cmd failed and is owned by EH */
	ATA_QCFLAG_SENSE_VALID	= (1 << 17), /* sense data valid */
	ATA_QCFLAG_EH_SCHEDULED = (1 << 18), /* EH scheduled (obsolete) */

	/* host set flags */
	ATA_HOST_SIMPLEX	= (1 << 0),	/* Host is simplex, one DMA channel per host only */
	ATA_HOST_STARTED	= (1 << 1),	/* Host started */
	ATA_HOST_PARALLEL_SCAN	= (1 << 2),	/* Ports on this host can be scanned in parallel */

	/* bits 24:31 of host->flags are reserved for LLD specific flags */

	/* various lengths of time */
	ATA_TMOUT_BOOT		= 30000,	/* heuristic */
	ATA_TMOUT_BOOT_QUICK	=  7000,	/* heuristic */
	ATA_TMOUT_INTERNAL_QUICK = 5000,
	ATA_TMOUT_MAX_PARK	= 30000,

	/* FIXME: GoVault needs 2s but we can't afford that without
	 * parallel probing.  800ms is enough for iVDR disk
	 * HHD424020F7SV00.  Increase to 2secs when parallel probing
	 * is in place.
	 */
	ATA_TMOUT_FF_WAIT	=  800,

	/* Spec mandates to wait for ">= 2ms" before checking status
	 * after reset.  We wait 150ms, because that was the magic
	 * delay used for ATAPI devices in Hale Landis's ATADRVR, for
	 * the period of time between when the ATA command register is
	 * written, and then status is checked.  Because waiting for
	 * "a while" before checking status is fine, post SRST, we
	 * perform this magic delay here as well.
	 *
	 * Old drivers/ide uses the 2mS rule and then waits for ready.
	 */
	ATA_WAIT_AFTER_RESET	=  150,

	/* If PMP is supported, we have to do follow-up SRST.  As some
	 * PMPs don't send D2H Reg FIS after hardreset, LLDs are
	 * advised to wait only for the following duration before
	 * doing SRST.
	 */
	ATA_TMOUT_PMP_SRST_WAIT	= 5000,

	/* ATA bus states */
	BUS_UNKNOWN		= 0,
	BUS_DMA			= 1,
	BUS_IDLE		= 2,
	BUS_NOINTR		= 3,
	BUS_NODATA		= 4,
	BUS_TIMER		= 5,
	BUS_PIO			= 6,
	BUS_EDD			= 7,
	BUS_IDENTIFY		= 8,
	BUS_PACKET		= 9,

	/* SATA port states */
	PORT_UNKNOWN		= 0,
	PORT_ENABLED		= 1,
	PORT_DISABLED		= 2,

	/* encoding various smaller bitmaps into a single
	 * unsigned long bitmap
	 */
	ATA_NR_PIO_MODES	= 7,
	ATA_NR_MWDMA_MODES	= 5,
	ATA_NR_UDMA_MODES	= 8,

	ATA_SHIFT_PIO		= 0,
	ATA_SHIFT_MWDMA		= ATA_SHIFT_PIO + ATA_NR_PIO_MODES,
	ATA_SHIFT_UDMA		= ATA_SHIFT_MWDMA + ATA_NR_MWDMA_MODES,

	/* size of buffer to pad xfers ending on unaligned boundaries */
	ATA_DMA_PAD_SZ		= 4,

	/* ering size */
	ATA_ERING_SIZE		= 32,

	/* return values for ->qc_defer */
	ATA_DEFER_LINK		= 1,
	ATA_DEFER_PORT		= 2,

	/* desc_len for ata_eh_info and context */
	ATA_EH_DESC_LEN		= 80,

	/* reset / recovery action types */
	ATA_EH_REVALIDATE	= (1 << 0),
	ATA_EH_SOFTRESET	= (1 << 1), /* meaningful only in ->prereset */
	ATA_EH_HARDRESET	= (1 << 2), /* meaningful only in ->prereset */
	ATA_EH_RESET		= ATA_EH_SOFTRESET | ATA_EH_HARDRESET,
	ATA_EH_ENABLE_LINK	= (1 << 3),
	ATA_EH_LPM		= (1 << 4),  /* link power management action */
	ATA_EH_PARK		= (1 << 5), /* unload heads and stop I/O */

	ATA_EH_PERDEV_MASK	= ATA_EH_REVALIDATE | ATA_EH_PARK,
	ATA_EH_ALL_ACTIONS	= ATA_EH_REVALIDATE | ATA_EH_RESET |
				  ATA_EH_ENABLE_LINK | ATA_EH_LPM,

	/* ata_eh_info->flags */
	ATA_EHI_HOTPLUGGED	= (1 << 0),  /* could have been hotplugged */
	ATA_EHI_NO_AUTOPSY	= (1 << 2),  /* no autopsy */
	ATA_EHI_QUIET		= (1 << 3),  /* be quiet */

	ATA_EHI_DID_SOFTRESET	= (1 << 16), /* already soft-reset this port */
	ATA_EHI_DID_HARDRESET	= (1 << 17), /* already soft-reset this port */
	ATA_EHI_PRINTINFO	= (1 << 18), /* print configuration info */
	ATA_EHI_SETMODE		= (1 << 19), /* configure transfer mode */
	ATA_EHI_POST_SETMODE	= (1 << 20), /* revaildating after setmode */

	ATA_EHI_DID_RESET	= ATA_EHI_DID_SOFTRESET | ATA_EHI_DID_HARDRESET,

	/* mask of flags to transfer *to* the slave link */
	ATA_EHI_TO_SLAVE_MASK	= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET,

	/* max tries if error condition is still set after ->error_handler */
	ATA_EH_MAX_TRIES	= 5,

	/* sometimes resuming a link requires several retries */
	ATA_LINK_RESUME_TRIES	= 5,

	/* how hard are we gonna try to probe/recover devices */
	ATA_PROBE_MAX_TRIES	= 3,
	ATA_EH_DEV_TRIES	= 3,
	ATA_EH_PMP_TRIES	= 5,
	ATA_EH_PMP_LINK_TRIES	= 3,

	SATA_PMP_RW_TIMEOUT	= 3000,		/* PMP read/write timeout */

	/* This should match the actual table size of
	 * ata_eh_cmd_timeout_table in libata-eh.c.
	 */
	ATA_EH_CMD_TIMEOUT_TABLE_SIZE = 6,

	/* Horkage types. May be set by libata or controller on drives
	   (some horkage may be drive/controller pair dependant */

	ATA_HORKAGE_DIAGNOSTIC	= (1 << 0),	/* Failed boot diag */
	ATA_HORKAGE_NODMA	= (1 << 1),	/* DMA problems */
	ATA_HORKAGE_NONCQ	= (1 << 2),	/* Don't use NCQ */
	ATA_HORKAGE_MAX_SEC_128	= (1 << 3),	/* Limit max sects to 128 */
	ATA_HORKAGE_BROKEN_HPA	= (1 << 4),	/* Broken HPA */
	ATA_HORKAGE_DISABLE	= (1 << 5),	/* Disable it */
	ATA_HORKAGE_HPA_SIZE	= (1 << 6),	/* native size off by one */
	ATA_HORKAGE_IPM		= (1 << 7),	/* Link PM problems */
	ATA_HORKAGE_IVB		= (1 << 8),	/* cbl det validity bit bugs */
	ATA_HORKAGE_STUCK_ERR	= (1 << 9),	/* stuck ERR on next PACKET */
	ATA_HORKAGE_BRIDGE_OK	= (1 << 10),	/* no bridge limits */
	ATA_HORKAGE_ATAPI_MOD16_DMA = (1 << 11), /* use ATAPI DMA for commands
						    not multiple of 16 bytes */
	ATA_HORKAGE_FIRMWARE_WARN = (1 << 12),	/* firmware update warning */
	ATA_HORKAGE_1_5_GBPS	= (1 << 13),	/* force 1.5 Gbps */
	ATA_HORKAGE_NOSETXFER	= (1 << 14),	/* skip SETXFER, SATA only */
	ATA_HORKAGE_BROKEN_FPDMA_AA	= (1 << 15),	/* skip AA */

	 /* DMA mask for user DMA control: User visible values; DO NOT
	    renumber */
	ATA_DMA_MASK_ATA	= (1 << 0),	/* DMA on ATA Disk */
	ATA_DMA_MASK_ATAPI	= (1 << 1),	/* DMA on ATAPI */
	ATA_DMA_MASK_CFA	= (1 << 2),	/* DMA on CF Card */

	/* ATAPI command types */
	ATAPI_READ		= 0,		/* READs */
	ATAPI_WRITE		= 1,		/* WRITEs */
	ATAPI_READ_CD		= 2,		/* READ CD [MSF] */
	ATAPI_PASS_THRU		= 3,		/* SAT pass-thru */
	ATAPI_MISC		= 4,		/* the rest */

	/* Timing constants */
	ATA_TIMING_SETUP	= (1 << 0),
	ATA_TIMING_ACT8B	= (1 << 1),
	ATA_TIMING_REC8B	= (1 << 2),
	ATA_TIMING_CYC8B	= (1 << 3),
	ATA_TIMING_8BIT		= ATA_TIMING_ACT8B | ATA_TIMING_REC8B |
				  ATA_TIMING_CYC8B,
	ATA_TIMING_ACTIVE	= (1 << 4),
	ATA_TIMING_RECOVER	= (1 << 5),
	ATA_TIMING_DMACK_HOLD	= (1 << 6),
	ATA_TIMING_CYCLE	= (1 << 7),
	ATA_TIMING_UDMA		= (1 << 8),
	ATA_TIMING_ALL		= ATA_TIMING_SETUP | ATA_TIMING_ACT8B |
				  ATA_TIMING_REC8B | ATA_TIMING_CYC8B |
				  ATA_TIMING_ACTIVE | ATA_TIMING_RECOVER |
				  ATA_TIMING_DMACK_HOLD | ATA_TIMING_CYCLE |
				  ATA_TIMING_UDMA,

	/* ACPI constants */
	ATA_ACPI_FILTER_SETXFER	= 1 << 0,
	ATA_ACPI_FILTER_LOCK	= 1 << 1,
	ATA_ACPI_FILTER_DIPM	= 1 << 2,
	ATA_ACPI_FILTER_FPDMA_OFFSET = 1 << 3,	/* FPDMA non-zero offset */
	ATA_ACPI_FILTER_FPDMA_AA = 1 << 4,	/* FPDMA auto activate */

	ATA_ACPI_FILTER_DEFAULT	= ATA_ACPI_FILTER_SETXFER |
				  ATA_ACPI_FILTER_LOCK |
				  ATA_ACPI_FILTER_DIPM,
};

enum ata_completion_errors {
  AC_ERR_DEV    = (1 << 0), /* device reported error */
  AC_ERR_HSM    = (1 << 1), /* host state machine violation */
  AC_ERR_TIMEOUT    = (1 << 2), /* timeout */
  AC_ERR_MEDIA    = (1 << 3), /* media error */
  AC_ERR_ATA_BUS    = (1 << 4), /* ATA bus error */
  AC_ERR_HOST_BUS   = (1 << 5), /* host bus error */
  AC_ERR_SYSTEM   = (1 << 6), /* system error */
  AC_ERR_INVALID    = (1 << 7), /* invalid argument */
  AC_ERR_OTHER    = (1 << 8), /* unknown */
  AC_ERR_NODEV_HINT = (1 << 9), /* polling device detection hint */
  AC_ERR_NCQ    = (1 << 10), /* marker for offending NCQ qc */
};

/* defines only for the constants which don't work well as enums */
#define ATA_TAG_POISON    0xfafbfcfdU

enum ata_tf_protocols {
	/* ATA taskfile protocols */
	ATA_PROT_UNKNOWN,	/* unknown/invalid */
	ATA_PROT_NODATA,	/* no data */
	ATA_PROT_PIO,		/* PIO data xfer */
	ATA_PROT_DMA,		/* DMA */
	ATA_PROT_NCQ,		/* NCQ */
	ATAPI_PROT_NODATA,	/* packet command, no data */
	ATAPI_PROT_PIO,		/* packet command, PIO data xfer*/
	ATAPI_PROT_DMA,		/* packet command with special DMA sauce */
};

/* core structures */
  
struct ata_prd {
  u32_t addr;
  u32_t flags_len;
};

struct ata_taskfile {
  unsigned long   flags;    /* ATA_TFLAG_xxx */
  u8_t      protocol; /* ATA_PROT_xxx */
  
  u8_t      ctl;    /* control reg */
  
  u8_t      hob_feature;  /* additional data */
  u8_t      hob_nsect;  /* to support LBA48 */
  u8_t      hob_lbal;
  u8_t      hob_lbam;
  u8_t      hob_lbah;
  
  u8_t      feature;
  u8_t      nsect;
  u8_t      lbal;
  u8_t      lbam;
  u8_t      lbah;
  
  u8_t      device;   

  u8_t      command;  /* IO operation */
};

struct ata_port_info {
  unsigned long   flags;
  unsigned long   link_flags;
  unsigned long   pio_mask;
  unsigned long   mwdma_mask;
  unsigned long   udma_mask;
  struct ata_port_operations *port_ops;
  void      *private_data;
};


/* Fields between ATA_DEVICE_CLEAR_BEGIN and ATA_DEVICE_CLEAR_END are
 * cleared to zero on ata_dev_init().
 */
#define ATA_DEVICE_CLEAR_BEGIN    offsetof(struct ata_device, n_sectors)
#define ATA_DEVICE_CLEAR_END    offsetof(struct ata_device, ering)

struct ata_device {
	struct ata_link *link;
	unsigned int devno;
	unsigned long flags;
	unsigned int horkage;

	u64_t n_sectors;
  u64_t n_native_sectors; /* native size, if ATA */

  u8_t      pio_mode;
  u8_t      dma_mode;
  u8_t      xfer_mode;

  unsigned int    multi_count;  /* sectors count for
               READ/WRITE MULTIPLE */
  unsigned int    max_sectors;  /* per-device max sectors */
  unsigned int    cdb_len;

  unsigned int    pio_mask;
  unsigned int    mwdma_mask;
  unsigned int    udma_mask;
	unsigned int class;

  /* for CHS addressing */
  u16_t     cylinders;  /* Number of cylinders */
  u16_t     heads;    /* Number of heads */
  u16_t     sectors;  /* Number of sectors per track */

  union {
    u16_t   id[ATA_ID_WORDS]; /* IDENTIFY xxx DEVICE data */
    u32_t   gscr[SATA_PMP_GSCR_DWORDS]; /* PMP GSCR block */
  };

	u8_t ering;
};

struct ata_eh_info {
  struct ata_device *dev;   /* offending device */
  u32_t     serror;   /* SError from LLDD */
  unsigned int    err_mask; /* port-wide err_mask */
  unsigned int    action;   /* ATA_EH_* action mask */
  unsigned int    dev_action[ATA_MAX_DEVICES]; /* dev EH action */
  unsigned int    flags;    /* ATA_EHI_* flags */

  unsigned int    probe_mask;

  char      desc[ATA_EH_DESC_LEN];
  int     desc_len;
};

struct ata_eh_context {
  struct ata_eh_info  i;
  int     tries[ATA_MAX_DEVICES];
  int     cmd_timeout_idx[ATA_MAX_DEVICES]
                 [ATA_EH_CMD_TIMEOUT_TABLE_SIZE];
  unsigned int    classes[ATA_MAX_DEVICES];
  unsigned int    did_probe_mask;
  unsigned int    unloaded_mask;
  unsigned int    saved_ncq_enabled;
  u8_t      saved_xfer_mode[ATA_MAX_DEVICES];
  /* timestamp for the last reset attempt or success */
  unsigned long   last_reset;
};

struct ata_link {
	struct ata_port *ap;
	int pmp;

	unsigned int active_tag;
  u32_t     sactive;  /* active NCQ commands */

	unsigned int flags;
	u32_t 			 saved_scontrol; /*SControl on Probe*/
	unsigned int hw_sata_spd_limit;
	unsigned int sata_spd_limit;
	unsigned int sata_spd;

  /* record runtime error info, protected by host_set lock */
  struct ata_eh_info  eh_info;
  /* EH context */
  struct ata_eh_context eh_context;

	struct ata_device device[ATA_MAX_DEVICES];
};

struct ata_ioports {
  void *cmd_addr;
  void *data_addr;
  void *error_addr;
  void *feature_addr;
  void *nsect_addr;
  void *lbal_addr;
  void *lbam_addr;
  void *lbah_addr;
  void *device_addr;
  void *status_addr;
  void *command_addr;
  void *altstatus_addr;
  void *ctl_addr;
  void *bmdma_addr;
  void *scr_addr;
};

/* typedefs */
typedef void (*ata_qc_cb_t) (void *qc);
typedef int (*ata_prereset_fn_t)(struct ata_link *link, unsigned long deadline);
typedef int (*ata_reset_fn_t)(struct ata_link *link, unsigned int *classes,
            unsigned long deadline);
typedef void (*ata_postreset_fn_t)(struct ata_link *link, unsigned int *classes);


struct ata_queued_cmd {
  struct ata_port   *ap;
  struct ata_device *dev;

  struct scsi_cmnd  *scsicmd;
  void      (*scsidone)(struct scsi_cmnd *);
	struct buf_head *bh;

  struct ata_taskfile tf;
  u8_t      cdb[ATAPI_CDB_LEN];

  unsigned long   flags;    /* ATA_QCFLAG_xxx */
  unsigned int    tag;
  unsigned int    n_elem;
  unsigned int    orig_n_elem;

  int     dma_dir;

  unsigned int    sect_size;
              
  unsigned int    nbytes;
  unsigned int    extrabytes;
  unsigned int    curbytes;

  struct scatterlist  *cursg;
  unsigned int    cursg_ofs;

  struct scatterlist  sgent;

  struct scatterlist  *sg;

  unsigned int    err_mask;
  struct ata_taskfile result_tf;
  ata_qc_cb_t   complete_fn;

  void      *private_data;
  void      *lldd_task;
};

struct ata_port {
	struct ata_port_operations *ops;
	unsigned long flags;
	unsigned long pflags;

  struct ata_prd    *prd;  /* our SG list */
  u32_t prd_dma; /* and its DMA mapping */

	struct ata_ioports ioaddr;	

  u8_t      ctl;  /* cache of ATA control register */
  u8_t      last_ctl; /* Cache last written value */
  unsigned int pio_mask;
  unsigned int mwdma_mask;
  unsigned int udma_mask;
	unsigned int cbl;

  struct ata_queued_cmd qcmd[ATA_MAX_QUEUE];
  unsigned long   qc_allocated;
  unsigned int    qc_active;
  int     nr_active_links; /* #links with active qcs */
	int 		print_id;

  unsigned int    hsm_task_state;

	unsigned int port_no;
	struct ata_link	link;
	struct ata_link *slave_link;

  int     nr_pmp_links; /* nr of available PMP links */
  struct ata_link   *pmp_link;  /* array of PMP links */
  struct ata_link   *excl_link; /* for PMP qc exclusion */

	struct ata_host *host;
	struct pci_device *dev;

  /* owned by EH */
  u8_t      sector_buf[ATA_SECT_SIZE];
};

struct ata_host { 
  struct pci_device     *dev;
  void  *iomap[PCI_ROM_RESOURCE];
  unsigned int    n_ports;
  struct ata_port_operations *ops;
  unsigned long   flags;
//  struct ata_port   *simplex_claimed; /* channel owning the DMA */
  struct ata_port   *ports[2];
};

struct ata_port_operations {
  /*
   * Command execution
   */
//  int  (*qc_defer)(struct ata_queued_cmd *qc);
//  int  (*check_atapi_dma)(struct ata_queued_cmd *qc);
  void (*qc_prep)(struct ata_queued_cmd *qc);
  unsigned int (*qc_issue)(struct ata_queued_cmd *qc);
  bool (*qc_fill_rtf)(struct ata_queued_cmd *qc);

  void (*set_piomode)(struct ata_port *ap, struct ata_device *dev);
  unsigned int (*read_id)(struct ata_device *dev, struct ata_taskfile *tf, u16_t *id);

  int  (*port_start)(struct ata_port *ap);

  void (*freeze)(struct ata_port *ap);
  void (*thaw)(struct ata_port *ap);
  ata_prereset_fn_t prereset;
  ata_reset_fn_t    softreset;
  ata_reset_fn_t    hardreset;
  ata_postreset_fn_t  postreset;


  void (*sff_dev_select)(struct ata_port *ap, unsigned int device);
  u8_t   (*sff_check_status)(struct ata_port *ap);
  u8_t   (*sff_check_altstatus)(struct ata_port *ap);
  void (*sff_tf_load)(struct ata_port *ap, const struct ata_taskfile *tf);
  void (*sff_tf_read)(struct ata_port *ap, struct ata_taskfile *tf);
  void (*sff_exec_command)(struct ata_port *ap,
         const struct ata_taskfile *tf);
  unsigned int (*sff_data_xfer)(struct ata_device *dev,
      unsigned char *buf, unsigned int buflen, int rw);

  void (*bmdma_setup)(struct ata_queued_cmd *qc);
  void (*bmdma_start)(struct ata_queued_cmd *qc);
  void (*bmdma_stop)(struct ata_queued_cmd *qc);
  u8_t (*bmdma_status)(struct ata_port *ap);
  void (*error_handler)(struct ata_port *ap);
  void (*post_internal_cmd)(struct ata_queued_cmd *qc);

  void (*dev_config)(struct ata_device *dev);

  int  (*scr_read)(struct ata_link *link, unsigned int sc_reg, u32_t *val);
  int  (*scr_write)(struct ata_link *link, unsigned int sc_reg, u32_t val);

  u8_t   (*sff_irq_on)(struct ata_port *);
  void (*sff_irq_clear)(struct ata_port *);

  /*
   * ->inherits must be the last field and all the preceding
   * fields must be pointers.
   */
  const struct ata_port_operations  *inherits;
};

/*    
 * id tests
 */
#define ata_id_is_ata(id) (((id)[ATA_ID_CONFIG] & (1 << 15)) == 0)
#define ata_id_has_lba(id)  ((id)[ATA_ID_CAPABILITY] & (1 << 9))
#define ata_id_has_dma(id)  ((id)[ATA_ID_CAPABILITY] & (1 << 8))
#define ata_id_has_ncq(id)  ((id)[76] & (1 << 8))
#define ata_id_queue_depth(id)  (((id)[ATA_ID_QUEUE_DEPTH] & 0x1f) + 1)
#define ata_id_removeable(id) ((id)[ATA_ID_CONFIG] & (1 << 7))
#define ata_id_has_atapi_AN(id) \
  ( (((id)[76] != 0x0000) && ((id)[76] != 0xffff)) && \
    ((id)[78] & (1 << 5)) )
#define ata_id_has_fpdma_aa(id) \
  ( (((id)[76] != 0x0000) && ((id)[76] != 0xffff)) && \
    ((id)[78] & (1 << 2)) )
#define ata_id_iordy_disable(id) ((id)[ATA_ID_CAPABILITY] & (1 << 10))
#define ata_id_has_iordy(id) ((id)[ATA_ID_CAPABILITY] & (1 << 11))
#define ata_id_u32(id,n)  \
  (((u32_t) (id)[(n) + 1] << 16) | ((u32_t) (id)[(n)]))
#define ata_id_u64(id,n)  \
  ( ((u64_t) (id)[(n) + 3] << 48) | \
    ((u64_t) (id)[(n) + 2] << 32) | \
    ((u64_t) (id)[(n) + 1] << 16) | \
    ((u64_t) (id)[(n) + 0]) )
     
#define ata_id_cdb_intr(id) (((id)[ATA_ID_CONFIG] & 0x60) == 0x20)

static inline unsigned int ata_prot_flags(u8_t prot)
{
  switch (prot) {
  case ATA_PROT_NODATA:
    return 0;
  case ATA_PROT_PIO:
    return ATA_PROT_FLAG_PIO;
  case ATA_PROT_DMA:
    return ATA_PROT_FLAG_DMA;
  case ATA_PROT_NCQ:
    return ATA_PROT_FLAG_DMA | ATA_PROT_FLAG_NCQ;
  case ATAPI_PROT_NODATA:
    return ATA_PROT_FLAG_ATAPI;
  case ATAPI_PROT_PIO:
    return ATA_PROT_FLAG_ATAPI | ATA_PROT_FLAG_PIO;
  case ATAPI_PROT_DMA:
    return ATA_PROT_FLAG_ATAPI | ATA_PROT_FLAG_DMA;
  }
  return 0;
}

static inline int ata_is_atapi(u8_t prot)
{
  return ata_prot_flags(prot) & ATA_PROT_FLAG_ATAPI;
} 

static inline int ata_is_nodata(u8_t prot)
{
  return !(ata_prot_flags(prot) & ATA_PROT_FLAG_DATA);
} 

static inline int ata_is_pio(u8_t prot)
{        
  return ata_prot_flags(prot) & ATA_PROT_FLAG_PIO;
}     

static inline int ata_is_dma(u8_t prot)
{ 
  return ata_prot_flags(prot) & ATA_PROT_FLAG_DMA;
}   
    
static inline int ata_is_ncq(u8_t prot)
{ 
  return ata_prot_flags(prot) & ATA_PROT_FLAG_NCQ;
} 

static inline int ata_is_data(u8_t prot)
{ 
  return ata_prot_flags(prot) & ATA_PROT_FLAG_DATA;
} 

static inline int ata_id_has_large_logical_sectors(const u16_t *id)
{
  if ((id[ATA_ID_SECTOR_SIZE] & 0xc000) != 0x4000)
    return 0;
  return id[ATA_ID_SECTOR_SIZE] & (1 << 13);
} 
    
static inline u16_t ata_id_logical_per_physical_sectors(const u16_t *id)
{
  return 1 << (id[ATA_ID_SECTOR_SIZE] & 0xf);
}

static inline int ata_id_has_lba48(const u16_t *id)
{   
  if ((id[ATA_ID_COMMAND_SET_2] & 0xC000) != 0x4000)
    return 0;
  if (!ata_id_u64(id, ATA_ID_LBA_CAPACITY_2))
    return 0;
  return id[ATA_ID_COMMAND_SET_2] & (1 << 10);
}     
             
static inline int ata_id_lba48_enabled(const u16_t *id)
{     
  if (ata_id_has_lba48(id) == 0)
    return 0;
  if ((id[ATA_ID_CSF_DEFAULT] & 0xC000) != 0x4000)
    return 0;
  return id[ATA_ID_CFS_ENABLE_2] & (1 << 10); 
}

static inline int ata_id_current_chs_valid(const u16_t *id)
{   
  /* For ATA-1 devices, if the INITIALIZE DEVICE PARAMETERS command
     has not been issued to the device then the values of
     id[ATA_ID_CUR_CYLS] to id[ATA_ID_CUR_SECTORS] are vendor specific. */
  return (id[ATA_ID_FIELD_VALID] & 1) && /* Current translation valid */
    id[ATA_ID_CUR_CYLS] &&  /* cylinders in current translation */
    id[ATA_ID_CUR_HEADS] &&  /* heads in current translation */
    id[ATA_ID_CUR_HEADS] <= 16 &&
    id[ATA_ID_CUR_SECTORS];    /* sectors in current translation */
} 

/**     
 *  ata_id_major_version  - get ATA level of drive
 *  @id: Identify data
 *    
 *  Caveats:
 *    ATA-1 considers identify optional
 *    ATA-2 introduces mandatory identify
 *    ATA-3 introduces word 80 and accurate reporting
 *  
 *  The practical impact of this is that ata_id_major_version cannot
 *  reliably report on drives below ATA3.
 */

static inline unsigned int ata_id_major_version(const u16_t *id)
{
  unsigned int mver;
  
  if (id[ATA_ID_MAJOR_VER] == 0xFFFF) 
    return 0;
  
  for (mver = 14; mver >= 1; mver--)
    if (id[ATA_ID_MAJOR_VER] & (1 << mver))
      break;
  return mver;
} 

static inline int ata_id_has_flush_ext(const u16_t *id)
{       
  if ((id[ATA_ID_COMMAND_SET_2] & 0xC000) != 0x4000)
    return 0;
  return id[ATA_ID_COMMAND_SET_2] & (1 << 13);
} 

static inline int ata_id_has_tpm(const u16_t *id)
{   
  /* The TPM bits are only valid on ATA8 */
  if (ata_id_major_version(id) < 8)
    return 0;
  if ((id[48] & 0xC000) != 0x4000)
    return 0;    
  return id[48] & (1 << 0);
}  

static inline int atapi_cdb_len(const u16_t *dev_id)
{   
  u16_t tmp = dev_id[ATA_ID_CONFIG] & 0x3;
  switch (tmp) {
  case 0:   return 12;
  case 1:   return 16;
  default:  return -1;
  }     
}    

static inline int atapi_id_dmadir(const u16_t *dev_id)
{     
  return ata_id_major_version(dev_id) >= 7 && (dev_id[62] & 0x8000);
}   

static inline int atapi_command_packet_set(const u16_t *dev_id)
{   
  return (dev_id[ATA_ID_CONFIG] >> 8) & 0x1f;
}  

static inline int ata_id_hpa_enabled(const u16_t *id)
{     
  /* Yes children, word 83 valid bits cover word 82 data */
  if ((id[ATA_ID_COMMAND_SET_2] & 0xC000) != 0x4000)
    return 0;
  /* And 87 covers 85-87 */
  if ((id[ATA_ID_CSF_DEFAULT] & 0xC000) != 0x4000)
    return 0;
  /* Check command sets enabled as well as supported */
  if ((id[ATA_ID_CFS_ENABLE_1] & (1 << 10)) == 0)
    return 0; 
  return id[ATA_ID_COMMAND_SET_1] & (1 << 10);
}    

static inline int ata_id_is_sata(const u16_t *id)
{
  /*
   * See if word 93 is 0 AND drive is at least ATA-5 compatible
   * verifying that word 80 by casting it to a signed type --
   * this trick allows us to filter out the reserved values of
   * 0x0000 and 0xffff along with the earlier ATA revisions...
   */ 
  if (id[ATA_ID_HW_CONFIG] == 0 && (short)id[ATA_ID_MAJOR_VER] >= 0x0020)
    return 1;
  return 0;
}


static inline int ata_id_has_hipm(const u16_t *id)
{
  u16_t val = id[76];
      
  if (val == 0 || val == 0xffff)
    return 0;

  return val & (1 << 9);
}  

static inline int ata_id_has_dipm(const u16_t *id)
{              
  u16_t val = id[78];
               
  if (val == 0 || val == 0xffff)
    return 0;

  return val & (1 << 3);
} 

static inline int is_multi_taskfile(struct ata_taskfile *tf)
{
  return (tf->command == ATA_CMD_READ_MULTI) ||
         (tf->command == ATA_CMD_WRITE_MULTI) ||
         (tf->command == ATA_CMD_READ_MULTI_EXT) ||
         (tf->command == ATA_CMD_WRITE_MULTI_EXT) ||
         (tf->command == ATA_CMD_WRITE_MULTI_FUA_EXT);
}

static inline int ata_ok(u8_t status)
{     
  return ((status & (ATA_BUSY | ATA_DRDY | ATA_DF | ATA_DRQ | ATA_ERR))
      == ATA_DRDY);
}   

static inline int lba_28_ok(u64_t block, u32_t n_block)
{
  /* check the ending block number */
  return ((block + n_block) < ((u64_t)1 << 28)) && (n_block <= 256);
}   
      
static inline int lba_48_ok(u64_t block, u32_t n_block)
{   
  /* check the ending block number */
  return ((block + n_block - 1) < ((u64_t)1 << 48)) && (n_block <= 65536);
} 

extern void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
     unsigned int n_elem);
extern void ata_qc_issue(struct ata_queued_cmd *qc);
extern void pirq_enable_irq_ata();
extern struct page *virt_to_page(const void *vaddr);
extern struct ata_host *ahost;
#endif
